package com.zcxx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxx.common.PageInfo;
import com.zcxx.common.Result;
import com.zcxx.model.SysUser;
import com.zcxx.model.User;
import com.zcxx.service.SysUserService;
import com.zcxx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@MapperScan("com.zcxx.mapper")
@RequestMapping("/user")
@Api(tags = "用户管理", description = "管理系统用户的增删改查操作")
public class UserApiController {

    @Autowired
    private UserService userService;

    /**
     * 插入用户信息
     *
     * @param user 用户信息（通过请求体传递）
     * @return 返回操作结果的 Result 对象
     */
    @PostMapping
    @ApiOperation(value = "插入用户信息", notes = "创建一个新用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "用户插入成功"),
            @ApiResponse(code = 500, message = "用户插入失败")
    })
    public Result<String> insert(@ApiParam(name = "user", value = "用户信息", required = true) @RequestBody User user) {
        return Result.success(userService.register(user));
    }

    /**
     * 删除用户信息
     *
     * @param id 用户 ID
     * @return 返回操作结果的 Result 对象
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户信息", notes = "根据用户ID删除用户")
    @ApiResponses({
            @ApiResponse(code = 200, message = "用户删除成功"),
            @ApiResponse(code = 404, message = "用户不存在"),
            @ApiResponse(code = 500, message = "用户删除失败")
    })
    public Result<String> delete(@ApiParam(name = "id", value = "用户ID", required = true) @PathVariable Long id) {
        userService.removeById(id);
        return Result.success("用户删除成功");
    }

    /**
     * 更新用户信息
     *
     * @param id   用户 ID
     * @param user 用户信息（通过请求体传递）
     * @return 返回操作结果的 Result 对象
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新用户信息", notes = "根据用户ID更新用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "用户修改成功"),
            @ApiResponse(code = 404, message = "用户不存在"),
            @ApiResponse(code = 500, message = "用户修改失败")
    })
    public Result<String> update(
            @ApiParam(name = "id", value = "用户ID", required = true) @PathVariable Long id,
            @ApiParam(name = "user", value = "用户信息", required = true) @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
        return Result.success("用户修改成功");
    }

    /**
     * 根据用户 ID 查询用户信息
     *
     * @param id 用户 ID
     * @return 返回包含用户信息的 Result 对象
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据用户 ID 查询用户信息", notes = "根据用户ID查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "用户不存在")
    })
    public Result<User> findById(@ApiParam(name = "id", value = "用户ID", required = true) @PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    // 获取所有用户
    @GetMapping
    @ApiOperation(value = "获取所有用户", notes = "查询所有用户信息，返回用户列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功")
    })
    public Result<List<User>> getAllUsers() {
        return Result.success(userService.list());
    }

    // 分页查询用户信息
    @GetMapping("/page")
    @ApiOperation(value = "分页查询用户信息", notes = "分页查询用户信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功")
    })
    public Result<IPage<User>> getUsers(
            @ApiParam(name = "pageNum", value = "当前页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum, // 当前页码，默认为 1
            @ApiParam(name = "pageSize", value = "每页条数", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize // 每页条数，默认为 10
    ) {
        IPage<User> page = new Page<>(pageNum, pageSize); // 创建分页对象
        return Result.success(userService.page(page)); // 调用 Service 的分页方法
    }
}
